package com.adminpanel.action;



import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;


import com.adminpanel.modelbean.UserBean;
import com.adminpanel.service.LogServices;
import com.adminpanel.service.UserServices;
import com.adminpanel.util.Util;


@Controller
@SessionAttributes("loginuser")
public class UserController {
	
	@Resource(name = "userServices")
	private UserServices userServices;
	
	@Resource(name = "logServices")
	private LogServices logServices;
   
	//判断是否有合法用户登录的session,有的话则重定向到dashboard页面，没有的话则重定向到login页面
	//是否有一个checkSession的功能
	@RequestMapping("index")
	//@ModelAttribute("loginuser")
	public String AdminIndex() {
		
		return "index";

	}
	
	
	
	@RequestMapping("register")
	public String Register(){
		return "register";
	}
	
	@RequestMapping("registeruser")
	public String add(UserBean userBean,HttpServletRequest request,ModelMap map) {
		
		userBean.setUsername(request.getParameter("username"));
		userBean.setPassword(request.getParameter("password"));
		userBean.setEmail(request.getParameter("email"));
		userServices.addUser(userBean);
		map.put("loginuser", userServices.findUserByName(request.getParameter("username")));
		userServices.sendlink(request.getParameter("email"), "active", makeActiveLink(request.getParameter("username")));
		return "redirect:activelink";
	}
	
	
	@RequestMapping("activelink")
		public String activelink(UserBean userBean,HttpServletRequest request,ModelMap map){
			userBean.setUsername(request.getParameter("username"));
			map.put("loginuser", userServices.findUserByName(request.getParameter("username")));
			return "activelink";
		}
	
	@RequestMapping("userinfo")
	public String getUserInfo(@ModelAttribute("loginuser") UserBean userBean,Model model){
		model.addAttribute("changepw", userServices.findUserByName(userBean.getUsername()));
		return "changepw";
	}
	
	
	@RequestMapping("changeuserpw")
	public String changpassword(@ModelAttribute("loginuser") UserBean userBean,HttpServletRequest request){
		//new DigestUtils();
		new DigestUtils();
		//model.addAttribute("changepw", userServices.updatepw(userBean.getUsername()));
		userServices.updatepw(DigestUtils.md5Hex(request.getParameter("new_password")),userBean.getUsername());
		return "redirect:index";
	}
	
	@RequestMapping(value="checkUsername",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkSameName(HttpServletResponse response,
			HttpServletRequest request) {
		String inputUsername = request.getParameter("username");
		int matchcount = userServices.findExistName(inputUsername);
		if (matchcount == 0) {

			return true;
		}

		return false;

	}
	
	@SuppressWarnings("static-access")
	public String makeActiveLink(String username){
		new DigestUtils();
		UserBean userBean = userServices.findUserByName(username);
		String loginUsername = userBean.getUsername();
		String pw = userBean.getPassword();
		String rt = userBean.getRegister_time();
		String token = new DigestUtils().md5Hex(loginUsername+pw);
		String exp =  new Util().encode64_time(rt);
		String fulllink = "http://localhost:8080/adminpanel/active?u="+loginUsername+"&token="+token+"&exp="+exp;
		return fulllink;
	}
	
	
	
	
	@RequestMapping("logout")
	public String Logout(@ModelAttribute("loginuser") UserBean userBean,SessionStatus status,HttpServletRequest request){
		
		String action_name = request.getServletPath();
		String create_datetime = new Util().getTime();
		logServices.addLog(userBean.getId(), action_name, create_datetime);
		status.setComplete();
		return "login";
	}
	
	public int checkStatus(String username){
		UserBean userBean = userServices.findUserByName(username);
		int mystatus = userBean.getStatus();
		return mystatus;
	}
	
	@RequestMapping("login")
	public String login(){
		
		return "login";
	}
	
	@RequestMapping(value="dologin",method=RequestMethod.POST)
	public String doLogin(HttpServletRequest request,ModelMap map,Model model){
		UserBean userBean = new UserBean();
		userBean.setUsername(request.getParameter("username"));
		userBean.setPassword(request.getParameter("password"));
		userBean.setStatus(checkStatus(request.getParameter("username")));
		if(userServices.checkUser(userBean)){
			
			map.put("loginuser", userServices.findUserByName(request.getParameter("username")));
			UserBean login_userbean = userServices.findUserByName(request.getParameter("username"));
			int user_id = login_userbean.getId();
			String action_name = request.getServletPath();
			String create_datetime = new Util().getTime();
			logServices.addLog(user_id, action_name, create_datetime);
			String last_login_time = logServices.getLastRecord(user_id);
			model.addAttribute("last_login_time", last_login_time);
			return "index";
			
		}
		else{
			return "login";
		}
	}
	
	
//	@RequestMapping("lastlogin")
//	public String showlogin(@ModelAttribute("loginuser") UserBean userBean,Model model){
//		String last_login_time = logServices.getLastRecord(userBean.getId());
//		//ModelAndView mav=new ModelAndView();
//		//mav.setViewName("index.jsp");
//		//mav.addObject("last_login_time",last_login_time);
//		model.addAttribute("last_login_time", last_login_time);
//		return "index";
//	}
	
	
//	@RequestMapping("adduser")
//	public String addUser(@ModelAttribute("user") UserBean userBean){
//		userServices.addUser(userBean);
//		return "showtest";
//	}
	
	
}
